//统计小区车位情况
layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/community/getTotal",
        type: "get",
        success: function (result) {
            document.getElementById("parkTotal").innerHTML = result.parkTotal;
            document.getElementById("unCheckParkCount").innerHTML = result.unCheckParkCount;
            document.getElementById("unBusyParkCount").innerHTML = result.unBusyParkCount;
            document.getElementById("busyParkCount").innerHTML = result.busyParkCount;



        }
    });
});


//统计小区分成
layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/community/getMoney",
        type: "get",
        success: function (result) {
            document.getElementById("money").innerHTML = result;



        }
    });
});

// 饼图
// 基于准备好的dom，初始化echarts实例
var myChart = echarts.init(document.getElementById('dataCount'));
var myChart1 = echarts.init(document.getElementById('score'));

// 指定图表的配置项和数据
var option = {
    tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
        orient: 'vertical',
        left: 'left',
        right: 10,
        top: 20,
        bottom: 10,
        data: ['物业','车辆','车位']
    },
    series: [
        {
            name:'用户租车',
            type:'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '25',
                        fontWeight: 'bold'
                    }
                }
            },
            labelLine: {
                normal: {
                    show: false
                }
            },
            data:[
                {value:100, name:'物业'},
                {value:100, name:'车辆'},
                {value:104, name:'车位'}
        ]
        }
    ]
}

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

myChart.showLoading();


layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/platform/count",
        type: "get",
        success: function (result) {
            document.getElementById("userCount").innerHTML = result.userCount-1;

            myChart.hideLoading();
            // 填入数据
            myChart.setOption({

                series: [{
            data:[
                {value:result.comCount, name:'物业'},
                {value:result.carCount, name:'车辆'},
                {value:result.carParkCount, name:'车位'}

            ]
        }]
            });

        }
    });
});



//  统计车位评分的柱形图

var option1 = {
    title: {
        text: '小区车位评分',

    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'shadow'
        }
    },
    legend: {
        data: ['次数']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        type: 'value',
        boundaryGap: [0, 5]
    },
    yAxis: {
        type: 'category',
        data: ['0分','1分','2分','3分','4分','5分']
    },
    series: [
        {
            name: '次数',
            type: 'bar',
            data: [10, 10,10, 10,10, 10]
        }

    ]
};
myChart1.setOption(option1);
myChart1.showLoading();
//
layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/community/getScore",
        type: "get",
        success: function (result) {

            myChart1.hideLoading();
            // 填入数据
            myChart1.setOption({

                series: [
                    {
                        name: '次数',
                        type: 'bar',
                        data: [result.count0, result.count1,result.count2, result.count3,result.count4, result.count5]
                    }

                ]
            });

        }
    });
});



// websocket

var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('websocket' in window) {
    var account=$("#account").val();

    websocket = new WebSocket("ws://localhost:80/sharepark/ws/onlineServer/"+account);

    //连接成功建立的回调方法
    websocket.onopen = function () {
        // alert("客户端连接成功");
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        // alert("客户端接收到消息");
        setMessageInnerHTML(event.data);
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        // alert("WebSocket连接发生错误");
    };

    //连接关闭的回调方法
    websocket.onclose = function () {
        // alert("WebSocket连接关闭");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
    }

}
else {
    // alert('当前浏览器 Not support websocket')
}

//将消息显示在网页上
function setMessageInnerHTML(count) {

    // $("#count").innerHTML(count);
    document.getElementById('onlineCount').innerHTML = count;


}

//关闭WebSocket连接
function closeWebSocket() {
    websocket.close();
}




